home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Sun Solutions 1997 April to September
/
Sun Solutions CD - APR '97 - SEP '97 (704-3778-12 Rev. H)(Sun Microsystems, Inc.)(1997).iso
/
products
/
OpenLink
/
sample.java
< prev
next >
Wrap
Text File
|
1997-01-17
|
4KB
|
140 lines
// =============================================================
//
// OpenLink Java/JDBC Sample program.
//
// version 1.0
//
// Note. This sample program will connect over the
// internet to retrieve data with no additional
// configuration needed.
//
// All you need to do is to ensure that the openlink
// jdbc drivers are in the classpath:
//
// CLASSPATH=$CLASSPATH;/java/lib/opljdbc.zip
// or
// CLASSPATH=%CLASSPATH%;C:\java\lib\opljdbc.zip
//
// depending on your environment and location of opljdbc files.
//
// =============================================================
// Setup the imports for the classfile.
import java.util.Properties;
import java.sql.*;
//declare the class for our example
class sample
{
//Routine to display the contents of a field.
public static void PrintField (String name)
{
if (name == null)
name = "NULL";
System.out.print (name + " ");
}
//Routine to execute a query serverside via the openlink jdbc driver
public static void
ExecuteQuery (Connection conn, String query)
throws Exception
{
// declare local objects
ResultSetMetaData meta;
Statement stmt;
ResultSet result;
int count;
// Screen comment...
System.out.println ("EXECUTE: " + query);
// Execute Query
stmt = conn.createStatement ();
result = stmt.executeQuery (query);
// Get Resultset information
meta = result.getMetaData ();
count = meta.getColumnCount ();
// Display Column Names of table
for (int c = 1; c <= count; c++)
PrintField (meta.getColumnName (c));
System.out.println ("\n--------------");
// Retrieve results
while (result.next())
{
for (int c = 1; c <= count; c++)
// Print Results on screen
PrintField (result.getString (c));
System.out.println ("");
}
stmt.close ();
System.out.println ("");
}
// Program entry point
public static void main (String argv[])
throws Exception
{
Connection conn;
// Add the OpenLink JDBC driver to the system properties
Properties p = System.getProperties ();
p.put ("jdbc.drivers", "openlink.jdbc.Driver");
System.setProperties (p);
// Connect to the Request Broker.
//
// If you have an internet connection, this
// URL will retrieve data across the internet
// from our UK Based Request Broker.
//
// The JDBC Agent is configured for a secure connection
// and will only connect to a low-privelege user
// on an Oracle (NT) database. You have select
// insert, update, create table and drop table
// privileges. You have access to the following
// tables: emp, dept, bonus, so the following queries
// should produce data:
//
// select * from emp
// select * from dept
// select * from bonus
//
// Note that the URL does not contain a username (UID) or password
// (PWD) or any other connection information.
//
// This is because the JDBC Agent is configured in +secure mode,
// and will ignore all other connection information other than the
// DataSourceName. The remainder of the connection information is
// gathered serverside from the OpenLink Rulebook. If the +secure
// option is _not_ used, it would be possible to submit connection
// information in the URL as per the documentation.
// Note also, the initial connection to the broker is on port
// 5000. This is the default for OpenLink JDBC. It is configurable.
// If there is a firewall in place, you will need to punch a hole
// for ports 5000 through 6000. For connections to the Request
// Broker, the initial connection is managed through port
// 5000 (default). The client classes will then connect to the
// jdbc agent on the next lowest available port within the range
// specified in the OpenLink Rulebook between the PortLow and
// PortHigh settings.
conn = DriverManager.getConnection (
"jdbc:openlink://oplweb.openlink.co.uk/DSN=publicDSN_Oracle/UID=jdbc/PWD=java");
// Assumes Oracle database ...
for (int i = 0; i < 10; i++)
// Execute Query: This can be changed.....
// ExecuteQuery (conn, "select * from dept");
ExecuteQuery (conn, "select * from emp");
}
}